"""
说明 ；计算auc，mcc
"""
from sklearn.metrics import roc_auc_score, matthews_corrcoef


def accu_auc(pri_data, tru_data, k):
    """ 处理一对多"""
    back_label_pri = []
    back_label_true = []
    for i in pri_data:
        if i == k:
            back_label_pri.append(1)
        else:
            back_label_pri.append(0)
    for i in tru_data:
        if i == k:
            back_label_true.append(1)
        else:
            back_label_true.append(0)
    return back_label_true, back_label_pri


def handle_auc_data(label_arg):
    """ 处理得到所有的auc多分类的值，进行处理"""
    back_list = []
    for d in label_arg:
        if d == 1:
            back_list.append([0, 1, 0])
        elif d == 0:
            back_list.append([1, 0, 0])
        else:
            back_list.append([0, 0, 1])
    return back_list


def handle_file(name):
    sum_data = []
    with open(name, 'r') as f:
        data = f.readlines()
        for i in data:
            p = eval(i)
            sum_data.append(p)
    return sum_data


if __name__ == '__main__':
    # filename = 'test.txt'  # 保存的数据的文件
    filename = 'score.txt'  # 保存的数据的文件
    sums_data = handle_file(filename)  # 读出文件中的数据
    print(len(sums_data))
    suns = 0
    true_labels = []  # 这些图片原始对应的标签，未转化为one-hot
    pri_labels = []  # 图片经过模型预测出来的标签，未转化为one-hot
    data_pri_three = []  # 所有的图片经过三分类模型得到的原始值
    for j in sums_data[:30]:
        if suns % 3 == 0:
            data_pri_three = data_pri_three + j
        if suns % 3 == 1:
            pri_labels = pri_labels + j
        if suns % 3 == 2:
            true_labels = true_labels + j
        suns = suns + 1
    print("十折交叉验证总样本数:", len(pri_labels))  # 十折交叉验证总样本数
    # 转化标签
    back_label_true0, back_label_pri0 = accu_auc(pri_labels, true_labels, 0)  # 0:[1,2]
    back_label_true1, back_label_pri1 = accu_auc(pri_labels, true_labels, 1)  # 1:[0,2]
    back_label_true2, back_label_pri2 = accu_auc(pri_labels, true_labels, 2)  # 2:[0,1]
    # 计算auc     0:nCT    1:oCT    2:tCT
    t0 = roc_auc_score(back_label_true0, back_label_pri0)
    t1 = roc_auc_score(back_label_true1, back_label_pri1)
    t2 = roc_auc_score(back_label_true2, back_label_pri2)
    # 计算mcc     0:nCT    1:oCT    2:tCT
    m0 = matthews_corrcoef(back_label_true0, back_label_pri0)
    m1 = matthews_corrcoef(back_label_true1, back_label_pri1)
    m2 = matthews_corrcoef(back_label_true2, back_label_pri2)
    print("nCT:", "auc=", t0, "mcc=", m0)
    print("oCT:", "auc=", t1, "mcc=", m1)
    print("tCT:", "auc=", t2, "mcc=", m2)

    # auc 三分类合
    back_list_three = handle_auc_data(true_labels)
    t4 = roc_auc_score(back_list_three, data_pri_three, multi_class='ovr')
    m4 = matthews_corrcoef(true_labels, pri_labels)
    print("总的：auc=", t4, "总的：mcc=", m4)

# 模型二
# 十折交叉验证总样本数: 10000
# nCT: auc= 0.9809205687942736 mcc= 0.9606646623819414
# oCT: auc= 0.9697501607684409 mcc= 0.9389416699042098
# tCT: auc= 0.9758866874301501 mcc= 0.9417915205239825
# 总的：auc= 0.9999987127762179 总的：mcc= 0.9470139767836069

# 模型一：
# 十折交叉验证总样本数: 18000
# NiCT: auc= 0.9952245450614948 mcc= 0.988630327086029
# nCT: auc= 0.9964086292869563 mcc= 0.9927803283470877
# pCT: auc= 0.9966719187112126 mcc= 0.9940418269657196
# 总的：auc= 0.9999998702224735 总的：mcc= 0.991736136645211
